package com.databaseprotect;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Design extends MainActivity {
    public String msg;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.design);
        Intent intent=getIntent();
        msg=intent.getStringExtra("data");
        new Thread(new Runnable() {
            @Override
            public void run() {
                Connection connection=null;
                connection=(Connection) DBHelper.getConn();
                Statement statement;
                try {
                    statement=connection.createStatement();
                    String sql="select * from meal where MID='"+msg+"';";
                    ResultSet resultSet=statement.executeQuery(sql);
                    while (resultSet.next()){
                        EditText name=(EditText)findViewById(R.id.design_name);
                        EditText introduce=(EditText)findViewById(R.id.design_introduce);
                        EditText price=(EditText)findViewById(R.id.design_price);
                        EditText num=(EditText)findViewById(R.id.design_num);
                        name.setText(resultSet.getString("name").toString());
                        introduce.setText(resultSet.getString("introduce").toString());
                        price.setText(resultSet.getString("price").toString());
                        num.setText(resultSet.getString("num").toString());
                    }
                    connection.close();
                    statement.close();
                    resultSet.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
    public void Update(View v){
        new Thread(new Runnable() {
            @Override
            public void run() {
                EditText name=(EditText)findViewById(R.id.design_name);
                EditText introduce=(EditText)findViewById(R.id.design_introduce);
                EditText price=(EditText)findViewById(R.id.design_price);
                EditText num=(EditText)findViewById(R.id.design_num);
                Connection conn = null;
                conn = (Connection) DBHelper.getConn();
                Statement statement;
                try {
                    statement=conn.createStatement();
                    String sql="update meal set name='"+name.getText().toString()+"' where MID='"+msg+"';";
                    statement.executeUpdate(sql);
                    sql="update meal set introduce='"+introduce.getText().toString()+"' where MID='"+msg+"';";
                    statement.executeUpdate(sql);
                    sql="update meal set price="+price.getText().toString()+" where MID='"+msg+"';";
                    statement.executeUpdate(sql);
                    sql="update meal set num="+num.getText().toString()+" where MID='"+msg+"';";
                    statement.executeUpdate(sql);
                    Message message = new Message();
                    message.what = 0;
                    handler.sendMessage(message);
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                Toast.makeText(Design.this, "保存成功", Toast.LENGTH_SHORT).show();
                finish();
                Intent intent=new Intent(Design.this,Admin_MainActivity.class);
                startActivity(intent);
            }
        }
    };
}
